package cn.tx.api.controller;

import cn.tx.api.common.CommonUtils;
import cn.tx.api.common.GetMacAddress;
import cn.tx.dubbo.client.OrderInfoService;
import cn.tx.dubbo.client.UserInfoService;
import cn.tx.order.feign.client.OrderClient;
import cn.tx.order.feign.fallback.OrderClientFallBack;
import cn.tx.order.feign.sentinel.OrderBlockHandler;
import cn.tx.pojo.domain.Order;
import cn.tx.pojo.domain.User;
import cn.tx.user.feign.client.UserClient;
import cn.tx.user.feign.fallback.UserClientFallBack;
import cn.tx.user.feign.sentinel.UserBlockHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Random;

/**
 * 接口测试：nacos+dubbo&nacos+feign
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    private static final Logger logger = LoggerFactory.getLogger(ApiController.class);

    @Autowired
    private UserClient userClient;

    @Autowired
    private OrderClient orderClient;

    //dubbo提供的Reference注解，用于调用远程服务
    @DubboReference(check = false)
    //@DubboReference(version = "",timeout = 0,interfaceClass = UserInfoService.class,check = false,)
    private UserInfoService userInfoService;

    //dubbo提供的Reference注解，用于调用远程服务
    @DubboReference(check = false)
    //@DubboReference(version = "",timeout = 0,interfaceClass = UserInfoService.class,check = false,)
    private OrderInfoService orderInfoService;


    /**
     * NO1.hello world
     * @return
     */
    @GetMapping("/hello")
    public String hello(){
        return "ApiApplication-gatewayController-[gateway/hello]-Hello ^_^";
    }

    /**
     * 获取电脑MAC地址1
     * @param request
     * @return
     */
    @GetMapping("/getMac")
    public String getMac(HttpServletRequest request){
        String ipAddress = "";
        if (request.getHeader("x-forwarded-for") == null) {
            ipAddress = request.getRemoteAddr();
            String macAddress = GetMacAddress.getMacAddress(ipAddress);
            return "ApiApplication-gatewayController-[gateway/getMac]-ip:" + ipAddress + " mac:"+macAddress;
        }
        ipAddress = request.getHeader("x-forwarded-for");
        String macAddress = GetMacAddress.getMacAddress(ipAddress);
        return "ApiApplication-gatewayController-[gateway/getMac]-ip:" + ipAddress + " mac:"+macAddress;
    }
    /**
     * 获取电脑MAC地址2
     * @param request
     * @return
     */
    @GetMapping("/getMac2")
    public String getMac2(HttpServletRequest request){
        String macInfo = CommonUtils.getMacInfo(request);
        String ipAddress = "";
        if (request.getHeader("x-forwarded-for") == null) {
            ipAddress = request.getRemoteAddr();
            //String macAddress = GetMacAddress.getMacAddress(ipAddress);
            String macInWindows = CommonUtils.getMacInWindows(ipAddress);
            return "ApiApplication-gatewayController-[gateway/getMac]-ip:" + ipAddress + " mac:"+macInWindows;
        }
        ipAddress = request.getHeader("x-forwarded-for");
        //String macAddress = GetMacAddress.getMacAddress(ipAddress);
        String macInWindows = CommonUtils.getMacInWindows(ipAddress);
        return "ApiApplication-gatewayController-[gateway/getMac]-ip:" + ipAddress + " mac:"+macInWindows;
    }

    /**
     * RPC调用 nacso+dubbo ===> user-server
     * @return
     */
    @GetMapping("/getUserInfo")
    public User getUserInfo(){
        return userInfoService.getUserInfo();
    }
    /**
     * nacso+dubbo===>user-server
     * @return
     */
    @GetMapping("/getOrderInfo")
    public Order getOrderInfo(){
        return orderInfoService.getOrderInfo();
    }

    /**
     * RPC调用 nacso+feign===>user-server
     * @return
     */
    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "getUserById" ,
            blockHandler = "getByIdBlockHandler",
            blockHandlerClass = UserBlockHandler.class,
            fallback = "getByIdFallback",
            fallbackClass = UserClientFallBack.class)
    public User getUserById(@PathVariable("id") Long id) {
        int i = 1 / new Random().nextInt(2);
        logger.info(i+"");
        return userClient.getUserById(id);
    }

    /**
     * 获取用户列表-userClient
     * @param params
     * @return
     */
    @GetMapping("/getListUser")
    @SentinelResource(value = "getListUser" ,
            blockHandler = "getListUserBlockHandler",
            blockHandlerClass = UserBlockHandler.class,
            fallback = "getListUserFallback",
            fallbackClass = UserClientFallBack.class)
    public Object getListUser(@RequestParam Map<String, Object> params) {
        logger.info("ApiApplication-ApiController-[api/getListOrder]-getListUser ^_^");
        return userClient.getListUser(params);
    }


    /**
     * 获取订单详情
     * nacso+feign===>order-server
     * @return
     */
    @GetMapping("/getOrderById/{id}")
    @SentinelResource(value = "getOrderById" ,
            blockHandler = "getByIdBlockHandler",
            blockHandlerClass = OrderBlockHandler.class,
            fallback = "getByIdFallback",
            fallbackClass = OrderClientFallBack.class)
    public Order getOrderById(@PathVariable("id") Long id) {
        int i = 1 / new Random().nextInt(2);
        logger.info(i+"");
        return orderClient.getOrderById(id);
    }

    /**
     * 获取订单列表-orderClient
     * @param params
     * @return
     */
    @GetMapping("/getListOrder")
    @SentinelResource(value = "getListOrder" ,
            blockHandler = "getListOrderBlockHandler",
            blockHandlerClass = OrderBlockHandler.class,
            fallback = "getListOrderFallback",
            fallbackClass = OrderClientFallBack.class)
    public Object getListOrder(@RequestParam Map<String, Object> params) {
        logger.info("ApiApplication-ApiController-[api/getListOrder]-Hello ^_^");
        return orderClient.getListOrder(params);
    }

}
